package com.zj.fx.controller.admin;

import com.zj.fx.common.base.BaseController;
import com.zj.fx.common.dataSources.DataSource;
import com.zj.fx.common.dataSources.DataSourceType;
import com.zj.fx.common.domain.AjaxResult;
import com.zj.fx.common.domain.ResultTable;
import com.zj.fx.common.log.Log;
import com.zj.fx.model.auto.SysDepartment;
import com.zj.fx.model.auto.TsysRole;
import com.zj.fx.model.auto.TsysUser;
import com.zj.fx.model.custom.*;
import com.zj.fx.model.info.InfoSubcompany;
import com.zj.fx.model.info.InfoSupply;
import com.zj.fx.service.SysDepartmentService;
import com.zj.fx.service.info.InfoSubcompanyService;
import com.zj.fx.service.info.InfoSupplyService;
import com.zj.fx.service.month.MonthSupplytaskService;
import com.zj.fx.shiro.util.ShiroUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(value = "用户数据")
@Controller
@RequestMapping("/UserController")
@RequiredArgsConstructor
public class UserController extends BaseController {

    private final String prefix = "admin/user";
    private final SysDepartmentService departmentService;

    @Autowired
    private InfoSupplyService infoSupplyService;

    @Autowired
    private InfoSubcompanyService infoSubcompanyService;

    /**
     * 展示跳转页面
     *
     * @param model
     * @return
     * @author fuce
     * @Date 2019年11月11日 下午4:14:34
     */
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/view")
    @RequiresPermissions("system:user:view")
    public String view(ModelMap model) {
        return prefix + "/list";
    }


    /**
     * list集合
     *
     * @param tablepar
     * @return
     * @author fuce
     * @Date 2019年11月11日 下午4:14:40
     */
    //@Log(title = "分页查询", action = "1")
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/list")
    @RequiresPermissions("system:user:list")
    @ResponseBody
    public ResultTable list(Tablepar tablepar,TsysUser tsysUser) {
        PageInfo<TsysUser> page = sysUserService.list(tablepar,tsysUser);
        return pageTable(page.getList(), page.getTotal());
    }

    /**
     * 新增跳转
     *
     * @param modelMap
     * @return
     * @author fuce
     * @Date 2019年11月11日 下午4:14:51
     */
    @ApiOperation(value = "新增跳转", notes = "新增跳转")
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        //添加角色列表
        List<TsysRole> tsysRoleList = sysRoleService.queryList();
        //部门列表
        List<SysDepartment> departments = departmentService.selectByExample2(new SysDepartment());
        //查询供电所列表
        List<InfoSupply> infoSupplies = infoSupplyService.queryList();
        //查询分公司列表
        List<InfoSubcompany> infoSubcompanies = infoSubcompanyService.queryList();
        //角色
        modelMap.put("tsysRoleList", tsysRoleList);
        //部门
        modelMap.put("depList", departments);
        //供电所
        modelMap.put("supplyList", infoSupplies);
        //分公司
        modelMap.put("subCompanyList", infoSubcompanies);
        return prefix + "/add";
    }

    /**
     * 新增保存
     *
     * @param user
     * @return
     * @author fuce
     * @Date 2019年11月11日 下午4:14:57
     */
    @Log(title = "用户新增", action = "111")
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/add")
    @RequiresPermissions("system:user:add")
    @ResponseBody
    public AjaxResult add(TsysUser user, @RequestParam(value = "roleIds", required = false) String roleIds, @RequestParam(value = "depIds", required = false) String depIds
            , @RequestParam(value = "subCompanyIds", required = false) String subCompanyIds) {
        user.setDepId(depIds);
        user.setSubcompanyId(subCompanyIds);
        int b = sysUserService.insertUserRoles(user, roleIds);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 删除用户
     *
     * @param ids
     * @return
     */
    //@Log(title = "删除用户", action = "1")
    @ApiOperation(value = "删除", notes = "删除")
    @DeleteMapping("/remove")
    @RequiresPermissions("system:user:remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        int b = sysUserService.deleteByPrimaryKey(ids);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 检查用户
     *
     * @param tsysUser
     * @return
     */
    @ApiOperation(value = "检查Name唯一", notes = "检查Name唯一")
    @PostMapping("/checkLoginNameUnique")
    @ResponseBody
    public int checkLoginNameUnique(TsysUser tsysUser) {
        int b = sysUserService.checkLoginNameUnique(tsysUser);
        if (b > 0) {
            return 1;
        } else {
            return 0;
        }
    }


    /**
     * 修改用户跳转
     *
     * @param id
     * @param mmap
     * @return
     */
    @ApiOperation(value = "修改跳转", notes = "修改跳转")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        //查询所有角色
        List<RoleVo> roleVos = sysUserService.getUserIsRole(id);
        //岗位列表
        mmap.put("roleVos", roleVos);
        //查询所有部门
        List<DepVo> depVos = sysUserService.getUserIsDep(id);
        //岗位列表
        mmap.put("depList", depVos);
        //查询所有供电所
        List<InfoSupplyVo> infoSupplyVos = infoSupplyService.getUserIsSupply(id);
        mmap.put("supplyList", infoSupplyVos);

        //查询分公司列表
        List<InfoSubcompanyVo> infoSubcompanies = infoSubcompanyService.getUserisSubCompany(id);
        mmap.put("subCompanyList", infoSubcompanies);

        mmap.put("TsysUser", sysUserService.selectByPrimaryKey(id));
        //岗位
        return prefix + "/edit";
    }

    /**
     * 修改保存用户
     */
    @ApiOperation(value = "修改保存用户", notes = "修改保存用户")
    @RequiresPermissions("system:user:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TsysUser tsysUser, @RequestParam(value = "roleIds", required = false) String roleIds,
                               @RequestParam(value = "depIds", required = false) String depIds,
                               @RequestParam(value = "supplyIds", required = false) String supplyIds,
                               @RequestParam(value = "subCompanyIds", required = false) String subCompanyIds) {
        tsysUser.setDepId(depIds);
        tsysUser.setSupplyId(supplyIds);
        tsysUser.setSubcompanyId(subCompanyIds);
        return toAjax(sysUserService.updateUserRoles(tsysUser, roleIds));
    }


    /**
     * 修改用户密码跳转
     *
     * @param id
     * @param mmap
     * @return
     */
    //@Log(title = "修改用户密码", action = "1")
    @ApiOperation(value = "修改用户密码跳转", notes = "修改用户密码跳转")
    @GetMapping("/editPwd/{id}")
    public String editPwd(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("TsysUser", sysUserService.selectByPrimaryKey(id));
        return prefix + "/editPwd";
    }

    /**
     * 修改保存用户
     */
    //@Log(title = "修改用户密码", action = "1")
    @ApiOperation(value = "修改用户密码", notes = "修改用户密码")
    @RequiresPermissions("system:user:editPwd")
    @PostMapping("/editPwd")
    @ResponseBody
    public AjaxResult editPwdSave(TsysUser tsysUser) {
        return toAjax(sysUserService.updateUserPassword(tsysUser));
    }

    /**
     * 修改用户密码跳转
     *
     * @param mmap
     * @return
     */
    @ApiOperation(value = "修改用户密码跳转", notes = "修改用户密码跳转")
    @GetMapping("/editPwd2")
    public String editPwd2(ModelMap mmap) {
        TsysUser user = ShiroUtils.getUser();
        mmap.put("TsysUser", sysUserService.selectByPrimaryKey(user.getId()));
        return prefix + "/editPwd2";
    }

    /**
     * 修改保存用户
     */
    @ApiOperation(value = "修改用户密码", notes = "修改用户密码")
    @PostMapping("/editPwd2")
    @ResponseBody
    public AjaxResult editPwdSave2(TsysUser tsysUser) {
        return toAjax(sysUserService.updateUserPassword(tsysUser));
    }


    @RequestMapping("CCCC")
    @ResponseBody
    @DataSource(value = DataSourceType.SLAVE)
    public int cccc() {
        return sysRoleService.CCCC();
    }
}
